<template>
  <el-card class="box-card">
    <div slot="header" class="clearfix">
      <shopee-row class="row">
        <shopee-col :span="6" vertical-align="center">
           <shopee-input v-model="project_name" disabled></shopee-input>
<!--          <div v-if="userPermission === 0">-->
<!--            <shopee-input v-model="project_name"></shopee-input>-->
<!--          </div>-->
<!--          <div v-else>-->
<!--            <shopee-input v-model="project_name" disabled></shopee-input>-->
<!--          </div>-->
        </shopee-col>
        <shopee-col :span="2" vertical-align="center">
          <div v-if="userPermission === 0">
            <el-button type="primary" @click="addProject(project_name)">添加项目</el-button>
          </div>
          <div v-else>
            <el-button type="primary" disabled>添加项目</el-button>
          </div>
        </shopee-col>
      </shopee-row>
      <br>
      <shopee-table :data="adminProjectDataTable" style="width: 40%">
        <shopee-table-column prop="id" label="序号" fixed="left" :width=100></shopee-table-column>
        <shopee-table-column prop="name" label="项目" :width=150></shopee-table-column>
        <shopee-table-column label="操作" :width="500">
          <template slot-scope="props">
            <el-button  size="small" type="primary" @click="goToPersion(props.row.id)">Members</el-button>
            <el-button  size="small" type="primary" disabled>Remove</el-button>
          </template>
        </shopee-table-column>
      </shopee-table>
    </div>
  </el-card>
</template>

  <script>
    import {api, getRequest, postRequest} from "@/api/commit";

    export default {
      name : "AdminProject",
      data() {
        return {
          adminProjectDataTable: [],
          userPermission: 3,
          project_name:""
        }
      },
      methods: {
        goToPersion: function(id){
          this.$router.push({
            path: '/AdminPersionView',
            query:{
              id:id
            }
          })
        },
        init_data() {
          let params = {
            "project_id":0
          }
          getRequest(api['permission_project_list']).then((res) => {
            res = res.resultReturn
            this.adminProjectDataTable= res.data
          }, (error) => {
            console.info(error)
          }),
          getRequest(api['permission_user'],params).then((res) => {
            res = res.resultReturn
            if (res.data.length===0){
              this.userPermission = 2
            }else{
              this.userPermission = res.data[0].type
            }
          }, (error) => {
            console.info(error)
            this.userPermission = 2
          })
        },
        addProject(project_name) {
          let params = {
            "project_name":project_name
          }
          postRequest(api['permission_project_add'],params).then((res) => {
            res = res.resultReturn
            this.init_data()
          }, (error) => {
            console.info(error)
          })
        },
      },
      created () {
        this.init_data()
      }
    }
  </script>
